package com.nondo.dean.dealer.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.auth.common.annotation.HasRole;
import com.nondo.dean.auth.common.bean.RoleEnum;
import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.AliyunOssTool;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.dealer.domain.Store;
import com.nondo.dean.dealer.service.StoreService;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/3/28
 */
@RestController("apiAdminDealerStore")
@RequestMapping("/{v}/admin/store")
public class StoreController {
    
    @Autowired
    AliyunOssTool aliyunOssTool;
    
    @Autowired
    StoreService storeService;

    /**
     * 列表
     *
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Store>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Store> query) {
        View<Page<Store>> view = new View<Page<Store>>(20000, "获取数据成功");

        Store store = query.getData();

        Page<Store> page = new Page<Store>(query.getPage(), query.getLimit(), storeService.count(store));

        List<Store> storeList = storeService.getList(query.getOffset(), query.getLimit(), store);

        for (Store storeDb : storeList) {
            storeDb.setLogo(aliyunOssTool.getImgUrl(storeDb.getLogo()));
        }
        
        page.setContent(storeList);

        view.setData(page);

        return view;
    }

    /**
     * 更新
     *
     * @param request
     * @param response
     * @param store
     * @param result
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<Store> update(HttpServletRequest request, HttpServletResponse response, @Validated @RequestBody Store store, BindingResult result) {
        View view = new View(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            this.storeService.update(store);
            store.setLogo(aliyunOssTool.getImgUrl(store.getLogo()));
        }

        return view;
    }

    /**
     * 删除
     *
     * @param request
     * @param response
     * @param categoryId
     * @return
     */
    @HasRole(RoleEnum.ADMIN)
    @ApiVersion(1)
    @RequestMapping("/delete/{id}")
    public View<Store> delete(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer id) {
        View view = new View(20000, "删除成功");

        Store store = this.storeService.select(id);
        this.storeService.delete(store);

        return view;
    }

}
